package com.itheima.leyou.queue;

import com.itheima.leyou.service.StorageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

@Component
public class StorageQueue {
    private static final String STORAGE_QUEUE = "storage_queue";

    @Resource
    StorageService storageService;

    private Logger logger = Logger.getLogger(String.valueOf(StorageQueue.class));

    @RabbitListener(queues = STORAGE_QUEUE)
    public void getStorageQueue(String msg){
        logger.info("接收消息："+msg);

        try {
            Map<String, Object> map = new HashMap<>();
            String[] strings = msg.split("_");
            if (strings.length==4){
                String warehouse_id = strings[0];
                String sku_id = strings[1];
                String inquanty = strings[2];
                String outquanty = strings[3];
                map = storageService.insertStorage(Integer.valueOf(warehouse_id),sku_id,Double.valueOf(inquanty),Double.valueOf(outquanty));
            }
            logger.info("队列处理完毕："+map);
        }catch (Exception e){
            logger.info(e.getMessage());
        }

    }
}
